package com.azh.server.vm;

import java.util.HashMap;
import java.util.Map;

import com.azh.server.tx.TxManagerImpl;

/**
 * @author zhaozhenhang <zhaozhenhang@kuaishou.com>
 * Created on 2022-04-30
 * versionManager 对 一个事物的抽象
 */
public class Tx {

    public long txId;

    public int level;

    public Map<Long, Boolean> snapshot;

    public Exception err;

    public boolean autoAborted;

    public static Tx newTx(long txId, int level, Map<Long, Tx> active) {
        Tx t = new Tx();
        t.txId = txId;
        t.level = level;
        if (level != 0) {
            t.snapshot = new HashMap<>();
            for (Long x : active.keySet()) {
                t.snapshot.put(x, true);
            }
        }
        return t;
    }

    public boolean isInSnapshot(long txId) {
        if (txId == TxManagerImpl.SUPER_TX_ID) {
            return false;
        }
        return snapshot.containsKey(txId);
    }
}
